package com.ktgj.customer.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.TravelContractDto;
import com.ktgj.customer.domain.vo.ContractListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.TravelContract;
import com.ktgj.customer.service.ITravelContractService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 合同2Controller
 * 
 * @author ruoyi
 * @date 2021-10-26
 */
@ControllerAdvice
@RestController
@RequestMapping("/contract")
public class TravelContractController extends BaseController
{
    @Autowired
    private ITravelContractService travelContractService;

    /**
     * 查询合同2列表
     */
    //@PreAuthorize(hasPermi = "customer:contract:list")
    @GetMapping("/list")
    public TableDataInfo list(TravelContractDto travelContractDto)
    {
        startPage();
        List<TravelContractDto> list = travelContractService.selectTravelContractList(travelContractDto);
        return getDataTable(list);
    }

    /**
     * 导出合同2列表
     */
    //@PreAuthorize(hasPermi = "customer:contract:export")
    @Log(title = "合同2", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TravelContractDto travelContractDto) throws IOException
    {
        List<TravelContractDto> list = travelContractService.selectTravelContractList(travelContractDto);
        ExcelUtil<TravelContractDto> util = new ExcelUtil<TravelContractDto>(TravelContractDto.class);
        util.exportExcel(response, list, "合同2数据");
    }

    /**
     * 获取合同2详细信息
     */
    //@PreAuthorize(hasPermi = "customer:contract:query")
    @GetMapping(value = "/{contractId}")
    public AjaxResult getInfo(@PathVariable("contractId") Long contractId)
    {
        return AjaxResult.success(travelContractService.selectTravelContractById(contractId));
    }

    /**
     * 新增合同2
     */
    //@PreAuthorize(hasPermi = "customer:contract:add")
    @Log(title = "合同2", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TravelContractDto travelContractDto)
    {
        return toAjax(travelContractService.insertTravelContract(travelContractDto));
    }

    /**
     * 修改合同2
     */
    //@PreAuthorize(hasPermi = "customer:contract:edit")
    @Log(title = "合同2", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TravelContractDto travelContractDto)
    {
        return toAjax(travelContractService.updateTravelContract(travelContractDto));
    }

    /**
     * 删除合同2
     */
    //@PreAuthorize(hasPermi = "customer:contract:remove")
    @Log(title = "合同2", businessType = BusinessType.DELETE)
	@DeleteMapping("/{contractIds}")
    public AjaxResult remove(@PathVariable Long[] contractIds)
    {
        return toAjax(travelContractService.deleteTravelContractByIds(contractIds));
    }

    /**
     * 供应商合同列表
     * */
    @PostMapping("/contractList")
    public TableDataInfo contractList(@RequestBody ContractListVo contractListVo){
        startPage();
        List<TravelContract> travelContractList;
        try {
            travelContractList = travelContractService.contractList(contractListVo);
        } catch (Exception e){
            return null;
        }
        return getDataTable(travelContractList);
    }

    /**
     * 添加合同
     * */
    @PostMapping("/addContract")
    public AjaxResult addContract(@RequestBody TravelContract travelContract){
        int i;
        try {
            i = travelContractService.addContract(travelContract);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (i > 0){
            return AjaxResult.success("编辑成功");
        }
        else {
            return AjaxResult.error("编辑失败");
        }
    }

    /**
     * 供应商合同号列表
     * */
    @PostMapping("/contractNum")
    public AjaxResult contractNum(@RequestBody Map<String, Long> map)
    {
        Long supplierId = map.get("supplierId");
        if (supplierId == null)
        {
            return AjaxResult.error("供应商ID为空！");
        }
        List<TravelContract> travelContractList = travelContractService.contractNum(supplierId);
        return AjaxResult.success(travelContractList);
    }
}
